package com.sqq.testdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sqq.testdemo.activity.IntentTestActivity;
import com.sqq.testdemo.activity.LaoutTestActivity;
import com.sqq.testdemo.activity.LoginActivity;
import com.sqq.testdemo.activity.UIWidgetActivity;

public class MainActivity extends Activity {

    private static final String TAG = "HomeActivity";
    private ListView lv_home;
    private MyAdapter adapter;

    private static String[] names = {
            "关于Intent两种方式的测试","UI控件测试","Android布局",
            "创建自定义控件","简单登录案例","手机杀毒",
            "缓存清理","高级工具","设置中心"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv_home = (ListView) findViewById(R.id.lv_home);
        //设置适配器
        adapter = new MyAdapter();
        lv_home.setAdapter(adapter);
        //为每一项item 添加事件监听器
        lv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null ;
                switch (position) {
                    case 0 :
                        intent = new Intent(MainActivity.this, IntentTestActivity.class);
                        startActivity(intent);
                        break;
                    case 1 :
                        intent = new Intent(MainActivity.this,UIWidgetActivity.class);
                        startActivity(intent);
                        break;
                    case 2 :
                        intent = new Intent(MainActivity.this, LaoutTestActivity.class);
                        startActivity(intent);
                        break;
                    case 3 :

                        break;
                    case 4 :
                        intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add :
                Toast.makeText(MainActivity.this,"您点击了添加按钮",Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove :
                Toast.makeText(MainActivity.this,"您点击了添加按钮",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }


    /**
     * 定义适配器类
     */
    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,R.layout.list_item_home,null);
            TextView tv_item = (TextView) view.findViewById(R.id.tv_item);

            tv_item.setHeight(80);
            tv_item.setText(names[position]);
            return view;
        }
    }


}
